<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue-组件化的组件访问$parent和$root</title>
</head>

<body>
    <div id="app">
        <h2>我是Vue</h2>
        <cpn></cpn>
    </div>
    <template id="cpn">
        <div>
            <h2>我是cpn</h2>
            <ccpn></ccpn>
        </div>
    </template>
    <template id="ccpn">
        <div>
            <h2>我是ccpn</h2>
            <button @click="btnClick">按&nbsp;钮</button>
        </div>
    </template>
    <script src="./js/vue.min.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message:'hello',
                name:'我是Vue实例的name'

            },

            // 注册组件
            components: {
                cpn: {
                    template: '#cpn',
                    data(){
                        return{
                            name:'我是cpn组件的name'
                        }
                    },
                    methods: {

                    },
                    components: {
                        ccpn: {
                            template: '#ccpn',
                            methods: {
                                btnClick() {
                                    // 1.访问父组件$parent  开发中使用非常少，与父组件耦合度高，复用性低
                                    // console.log(this.$parent);
                                    // console.log(this.$parent.name);

                                    // 2.访问根组件$root  使用较少，因为开发中Vue实例内的内容很少
                                    console.log(this.$root);
                                    console.log(this.$root.name);
                                    console.log(this.$root.message);
                                    

                                }
                            }

                        }
                    }

                }
            }

        })
    </script>
</body>

</html>